package com.emmmya.ocs.modules.system.setting.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.emmmya.ocs.modules.system.annotation.OperateLog;
import com.emmmya.ocs.common.constant.SettingConstant;
import com.emmmya.ocs.common.utils.ResultUtil;
import com.emmmya.ocs.common.vo.Result;
import com.emmmya.ocs.modules.system.setting.entity.EmailProperties;
import com.emmmya.ocs.modules.system.setting.entity.PageProperties;
import com.emmmya.ocs.modules.system.setting.entity.Setting;
import com.emmmya.ocs.modules.system.setting.service.SettingService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @description:
 * @author: 咕噜肉IT小店-黑医
 * @createDate: 2020-05-13
 * @version: 1.0
 */
@Controller
@RequestMapping("system")
public class SettingController {
    @Autowired
    SettingService settingService;

    @RequestMapping("/setting")
    public String setting(Model model){
        List<Setting> all = settingService.getAll();
        all.forEach(e->{
            String name = e.getKeySet();
            Class<?> clazz = null;
            switch (name){
                case SettingConstant.EMAIL_SETTING:
                    clazz =  EmailProperties.class;
                    break;
                case SettingConstant.PAGE_SETTING:
                    clazz =  PageProperties.class;
                    break;
                default:
                    break;
            }
            if(clazz!=null){
                model.addAttribute(name,JSON.parseObject(e.getValue(), clazz));
            }
        });
        return "admin2/system/sys-set";
    }


    @RequestMapping(value = "/set/{name}",method = RequestMethod.POST)
    @OperateLog(value="配置系统",operateType = 1,module = "系统设置模块")
    @ResponseBody
    public Result<Object> set(@PathVariable String name, @RequestBody(required = false) Map<String,Object> properties){
        try {
            if(properties == null && properties.size() >0){
                return ResultUtil.error("缺少配置信息");
            }
            Setting setting = new Setting();
            Setting ds_setting = null;
            switch (name){
                case SettingConstant.EMAIL_SETTING:
                    ds_setting = settingService.findByKeySet(SettingConstant.EMAIL_SETTING);
                    setting.setKeySet(SettingConstant.EMAIL_SETTING);
                    break;
                case SettingConstant.PAGE_SETTING:
                    ds_setting = settingService.findByKeySet(SettingConstant.PAGE_SETTING);
                    setting.setKeySet(SettingConstant.PAGE_SETTING);
                    break;
                default:
                    break;
            }
            Object o = JSONObject.toJSONString(properties);
            setting.setValue(o.toString());
            if(ds_setting == null){
                settingService.save(setting);
            }else{
                ds_setting.setValue(o.toString());
                settingService.update(ds_setting);
            }
            return ResultUtil.data(name,"保存成功！");
        } catch (Exception e) {
//            e.printStackTrace();
            return ResultUtil.error(e);
        }
    }

    @RequestMapping(value = "/get/{name}",method = RequestMethod.GET)
    @ResponseBody
    public Result<Object> get(@PathVariable String name){
        Setting byKeySet = settingService.findByKeySet(name);
        Class<?> clazz = null;
        switch (name){
            case SettingConstant.EMAIL_SETTING:
                clazz =  EmailProperties.class;
                break;
            case SettingConstant.PAGE_SETTING:
                clazz =  PageProperties.class;
                break;
            default:
                break;
        }
        if(clazz == null){
            return ResultUtil.error("未查询到该配置信息！");
        }
        return ResultUtil.data(JSON.parseObject(byKeySet.getValue(), clazz));
    }

}
